import Vue from "vue";
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: '/',
            meta: { hidden: true },
            redirect: '/home',
        }, {
            path: '/home',
            meta: { title: '首页', left: true, Index: 0 },
            component: () => import('../pages/home/index.vue')
        },
        {
            path: '/order',
            meta: { title: '订单列表', left: true, Index: 1 },
            component: () => import('../pages/order/index.vue')
        }, {
            path: '/evaluate',
            meta: { title: '评价列表', left: true, Index: 2 },
            component: () => import('../pages/evaluate/index.vue')
        }, {
            path: '/commodity',
            meta: { title: '商品管理', left: true, Index: 3 },
            component: () => import('../pages/commodity/index.vue')
        }, {
            path: '/client',
            meta: { title: '客户列表', left: true, Index: 4 },
            component: () => import('../pages/client/index.vue'),
            children: [
                {
                    path: '/client/xiang/:id',
                    name: 'Xiang',
                    meta: { title: '客户详情', left: true },
                    component: () => import('../pages/client/xiang/index.vue'),
                }
            ]
        }, {
            path: '/shop',
            meta: { title: '店铺设置', left: true, Index: 5 },
            component: () => import('../pages/shop/index.vue')
        }, {
            path: '/role',
            meta: { title: '角色管理', left: true, Index: 6 },
            component: () => import('../pages/role/index.vue')
        }, {
            path: '/login',
            meta: { hidden: true },
            component: () => import('../pages/login/login.vue')
        }, {
            path: '/forgetPass',
            meta: { hidden: true },
            component: () => import('../pages/login/forgetPass')
        }
    ]
})
export default router

router.beforeEach((to, from, next) => {
    if (to.meta.left) {
        if (sessionStorage.getItem('token')) {
            next()
        } else {
            next({
                path: '/login'
            })
        }
    } else {
        if (sessionStorage.getItem('token')) {
            next('/home')
        } else {
            next()
        }
    }
})
